#include "stdafx.h"
#include "RangFor.h"

#include <iostream>
#include <vector>
#include <algorithm>

void RangForAction(void)
{
	// Basic 10 elements integer array.
	int x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	// Range-based for loop to iterate through the array
	for (int y : x) { // Access by value using a copy decleard as specific type.
							// Not perfected.
		std::cout << y << " ";
	}
	std::cout << std::endl;

	// The auto keyword causes type interence to be used. Preferred.
	for (auto y : x) { // Copy of 'x', almost always undesirable
		std::cout << y << " ";
	}
	std::cout << std::endl;

	for (auto& y : x) { // Type inference by reference.
		// Observes and/or modifies in-place, Preferred when modify is needed.
		std::cout << y << " ";
	}
	std::cout << std::endl;

	for (const auto& y : x) { // Type interence by const reference
		// Observes in-place. Preferred when no modify is needed.
		std::cout << y << " ";
	}
	std::cout << std::endl;
	std::cout << "end of integer arry test" << std::endl << std::endl;

	// Create a vector object that contains 10 elements
	std::vector<double> v(10);
	int number(0);
	std::generate(std::begin(v), std::end(v), [&]() -> double { return  (0.14159 + number++); });

	// Range-based for loop to iterate through the vector, observing in-place.
	for (const auto& j : v) {
		std::cout << j << " ";
	}
	std::cout << std::endl;
	std::cout << "End of the vector test" << std::endl;
}

// References: http://msdn.microsoft.com/en-us/library/jj203382.aspx